<template>
    <div>
      <!--      面包屑导航-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!--  卡片视图区域-->
      <el-card class="box-card">

        <!--      搜索区域-->
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable
                      @clear="getGoodsList" @change="getGoodsList" >
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" @click="addGoodsPage">添加商品</el-button>
          </el-col>
        </el-row>

        <!--        用户列表区-->
        <el-table :data="goodsList" border stripe>
          <!--          索引列-->
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格（元）" width="95px"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
          <el-table-column prop="add_time" label="创建时间" width="140px">
            <template scope="scope">
              {{scope.row.add_time | dataFormate}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="130px">
            <!--            作用域插槽-->
            <template slot-scope="scope">
              <!--             修改按钮-->
              <!--              open-delay延迟出现，单位毫秒 enterable 鼠标是否可进入到 tooltip 中-->
              <el-tooltip effect="dark" content="修改商品" placement="top" :open-delay=openDelay :enterable="false">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goods_id)"></el-button>
              </el-tooltip>
              <!--              删除按钮-->
              <el-tooltip effect="dark" content="删除商品" placement="top" :open-delay=openDelay :enterable="false">
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="showDeltDialog(scope.row.goods_id)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <!--      分页区域-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[2, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background>
        </el-pagination>
      </el-card>

      <!--      修改用户弹框-->
      <el-dialog
        title="修改用户"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="resetForm('editFormRef')">
        <el-form :model="editForm" :rules="editRules" ref="editFormRef" label-width="70px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="editForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="editForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
        <el-button type="info" @click="resetForm('editFormRef')">重置</el-button>
        </span>
      </el-dialog>
    </div>
</template>

<style scoped>

</style>

<script>
export default {
  data () {
    return {
      // 获取商品列表的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      goodsList: [],
      // 添加用户弹框开关
      addDialogVisible: false,
      // 修改用户弹框开关
      editDialogVisible: false,
      openDelay: 500,
      // 修改表单数据
      editForm: {}
    }
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 修改商品弹框触发
    showEditDialog(id) {

    },
    // 添加商品
    async addGoodsPage() {
      this.$router.push('/goods/add')
    },
    // 删除商品弹框触发
    async showDeltDialog(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消了删除！')
      }
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //  刷新列表
      this.getGoodsList()
      this.$message.success(res.meta.msg)
    },
    //  监听pageSize改变事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //  监听页码值改变事件
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 表单重置功能
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  },
  created () {
    this.getGoodsList()
  }
}
</script>
